define(["jquery","bbcare","bch/app/doctorManager/schedule.servers"],function($,$bb,$servers){
	var schedule = {};

	var ajaxScheduleModifyCallBack = function(data,dom,flag){
		if( flag == "add" ){
			dom.addClass("on").attr("id",data.id).html('<i class="fa fa-user" aria-hidden="true" style="color:white;"></i>');
		}else if( flag == "del" ){
			dom.removeClass("on").removeAttr("id").html("");
		}
		$("#change").show();
        setTimeout(function(){
           	$("#change").hide();
           	schedule.init();
        },1500);
	}

	/**
	 * 修改排班
	 */
	var onModify = function(){
        var workDay = $(this).index() == 7 ? 0 : $(this).index();
        var firstChild = $(this).parent("tr").find("td:eq(0)").attr("class");
        var workPeriod = firstChild == "morning" ? 0 : firstChild == "afternoon" ? 1 : firstChild == "evening" ? 2 : null;
        var res = $(this).attr("class").indexOf(" on");
        var doctorName = JSON.parse(sessionStorage.account).alias;
        var params = {
    		"oprInfo":{
    			"opNote":"【"+doctorName + "】医生修改了排班",
    			"opType":"3",
    		},
    		"scheduleType":"1", // 按周排班
    		"workDay":workDay,
    		"workPeriod":workPeriod,
    	};
        if( res == -1 ){
        	params.oprInfo.opCode = "2006";
        	$servers.ajaxScheduleListAdd(params,ajaxScheduleModifyCallBack,$(this),"add");
        }else{
        	params.scheduleId = $(this).attr("id");
        	params.oprInfo.opCode = "2007";
        	$servers.ajaxScheduleDel(params,ajaxScheduleModifyCallBack,$(this),"del");
        }
	}

	/**
	 * 查询医生排班成功后的回调函数
	 */
	var ajaxScheduleListCallBack = function(data){
		/**
		 * scheduleType:排班类型：1按周排班，2按月排班，3按具体日期排班
         * workDay：工作日：按周排班，则用0-6表示周日到周六；按月排班，则用1-31表示；按日期排班，则用具体日期表示
         * workPeriod：按一天上下午及晚上排班，则用0，1，2分别表示上午、下午和晚上，若用具体时间排班，则用具体时间表示 添加参数
		 */
		for (var i = 0; i < data.rows.length; i++) {
			if( data.rows[i].schedule_type == "1" ){
				var workDay = data.rows[i].work_day;        //工作日
                var workPeriod = data.rows[i].work_period;  //时段
                var scheduleId = data.rows[i].id;           //排班id
                var day = $servers.work(workDay);
                $("." + day + workPeriod).addClass("on").attr("id",scheduleId).html('<i class="fa fa-user" aria-hidden="true" style="color:white;"></i>');
			}
		}
		$("#schedule-time > tr").find("td:gt(0)").unbind("click");
		$("#schedule-time > tr").find("td:gt(0)").click(onModify);
	}

	schedule.init = function(){
		$servers.ajaxScheduleList(ajaxScheduleListCallBack);
	}

	return schedule;
});